import {fileURLToPath, URL} from 'node:url'

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import postCssPxToRem from 'postcss-pxtorem'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {VantResolver} from '@vant/auto-import-resolver'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        'pinia',
        {
          'dingtalk-jsapi': [['*', '$dd']],
        },
        {
          echarts: [['*', '$echarts']],
        },
        {
          '@/http/index.js': [
            ['default', '$http'],
            ['get', '$get'],
            ['post', '$post'],
            ['put', '$put'],
            ['del', '$del'],
          ],
        },
      ],
      dirs: ['src/components', 'src/views', 'src/stores'],
      resolvers: [VantResolver()],
    }),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
  css: {
    postcss: {
      plugins: [
        postCssPxToRem({
          rootValue({file}) {
            return file.indexOf('vant') !== -1 ? 37.5 : 75
          },
          propList: ['*'],
        }),
      ],
    },
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  server: {
    hmr: {overlay: false},
    host: true,
    port: 9000,
    proxy: {
      '^/api|/bus/api': {
        target: 'http://172.30.200.202:8840',
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/api/, ''),
      },
    },
  },
})
